/**
 * 获取页面数据
 * url：请求路劲
 * param：提交参数
 * reload：是否刷新页面
 */
function getData(url, param={}, reload=''){
	$.ajax({  
		type:'post',    
	    url: url,  
	    data:'lid=' + param.lid,  
	    cache:false,  
	    dataType:'json',  
	    async:false,
	    success:function(res){
	    	if(res.msg){
	    		if(res.status == 'success'){
	    			showMsg('温馨提示', res.msg, reload);		    		
		    	}else{
		    		var msg = '网络错误，请刷新页面重试';
		    		if(res.msg){
		    			msg = res.msg;
		    		}
		    		showMsg('温馨提示', msg);
		    	}
	    	}else{
	    		resData = res;
	    	}
	    }  
	})
}


/**
 * =========================================================================
 * 倒计时计算，返回00:00:00形式倒计时
 * @param second
 */
function daojishi (){
	var hour   = '00';
	var minute = '00';
	var second = '00';
	if (running_time <= 0){
		$('#tag_runningtime').html(hour + ':' + minute + ':' + second);
		// 当前期号结束，获取下批期号信息， 并重新载入数据.
		var content_msg  = resData.shangqi.period + '期已截止<br/>';
			content_msg += '当前期号<font style="color: red;">' + resData.lotting.period + '</font><br/>';
			content_msg += '投注时请注意期号';
			showMsg('温馨提示', content_msg, '');
			setTimeout("pageInit()", 1000);
		return;
	}
	hour       = parseInt(running_time / (60 * 60));
	minute     = parseInt( (running_time - (60*60*hour)) / 60);
	second     = parseInt(running_time % (60));
	if(hour == 0){
		hour = '00';
	}else if(hour > 0 && hour < 10){
		hour = '0' + hour;
	}
	if(minute == 0){
		minute = '00';
	}else if(minute > 0 && minute < 10){
		minute = '0' + minute;
	}
	if(second == 0){
		second = '00';
	}else if (second > 0 && second < 10){
		second = '0' + second;
	}
	$('#tag_runningtime').html(hour + ':' + minute + ':' + second);
	running_time--;
	setTimeout("daojishi()", 1000);
}

/**
 * 上期开奖前面旋转2秒后展示开奖结果
 * @param seconds
 */
var shangqi_kj = 1;
var clear_kj_ready   = '';
function shangqi_kj_reday(seconds){
	if(seconds <= 0){
		shangqi_kaijiang(resData.shangqi.code);
		return;
	}
	if(!$.isEmptyObject(resData.shangqi.code)){
		// 如果上期有开奖，则准备展示上期开奖结果
		seconds = seconds - 50;
	}
	if(shangqi_kj > 11) shangqi_kj = 1;
	$('#id_shangqi_open em').html(shangqi_kj);
	shangqi_kj++;
	clear_kj_ready = setTimeout('shangqi_kj_reday(' + seconds + ', "' + resData.shangqi.code + '")', 50);
}

/**
 * 上期开奖结果，这里主要是js效果以及控制
 */
function shangqi_kaijiang(code_str){
	$('.tag_Award_Number').html('');
	var strLi = '';
	if(code_str == 'null' || !code_str){
		strLi = '<img src="/static/images/lottery/syxw/ballOpening.gif"/>';
		$('.tag_Award_Number').append(strLi);
		$('.tag_Award_Number').append(strLi);
		$('.tag_Award_Number').append(strLi);
		$('.tag_Award_Number').append(strLi);
		$('.tag_Award_Number').append(strLi);
		return;
	}
	var kaijiang_code = code_str.split(',');
//	for(var i = 0; i < 5; i++){ $('#id_shangqi_open em').eq(i).html(kaijiang_code[i]); }
	
	for(var i = 0; i < 5; i++){ 
		strLi = '<li>' + kaijiang_code[i] + '</li>';
		$('.tag_Award_Number').append(strLi);
	}
}
//倒计时结束提示弹框
function showMsg(title='温馨提示', msg, type){
	if(!title){return}
	switch(type){
		case 'bet':
			var bet_status = false;
			layer.open({
				title: title,
				content: msg,
				className: 'layui-m-tag-msg',
				btn: ['投注','取消'],
				yes: function (index){
					doBet();
		        	layer.close(index);
				}
			});
			break;
		case 'success':
			layer.open({
	            title: '温馨提示',
	            content: '<span style="color:red">投注成功</span>，您可以在我的账户查看注单详情',
	            className: 'layui-m-tag-msg',
	            btn: ['确定'],
	            yes: function (index){
					// 继续投注
	            	layer.close(index);
	            }
	        });
			break;
		default:
			layer.open({
				title: title,
				content: msg,
				className: 'layui-m-tag-msg',
				btn: ['确定'],
				yes: function (index){
					layer.close(index);
				}
			});
			break;
	}
}

/**
 * 获取上一期的开奖情况
 */
function getLastPerOpen(){
	var lastPerRequest = {'operation':'getPerInfo', 'pid':resData.shangqi.id};
	$.ajax({  
		type:'post',    
	    url: getLastData,  
	    data:'data=' + JSON.stringify(lastPerRequest),  
	    cache:false,  
	    dataType:'json',  
	    success:function(res){  
	    	if(res.perInfo.code){
	    		// 清除settimeout
	    		clearTimeout(clear_kj_ready);
	    		// 调用上期开奖
	    		shangqi_kaijiang(res.perInfo.code);
	    	}
	    }
	})
	setTimeout("getLastPerOpen()", 1000*60);
}

